package utils;

import dao.UserDao;
import model.Article;
import model.Meeting;
import model.User;
import pojo.PageEntity;
import service.UserService;

import java.util.List;
import java.util.Map;

public class PageUtil {

    public static PageEntity<User> createPage(Integer currPage, String realName, List<User> data, Integer totalCount){
        PageEntity<User> pageModel = new PageEntity<>();

        // 页大小
        Integer pageSize = pageModel.getPageSize();

        //页数
        Integer totalPage = totalCount%pageSize == 0 ? totalCount/pageSize : (totalCount/pageSize) + 1;

        Integer size = pageModel.getSize();

        //起始页
        Integer begin = currPage%size == 0 ? currPage - size + 1  : currPage - (currPage%size) + 1;

        //末页
        Integer end = begin + size - 1;
        if(end > totalPage){
            end = totalPage;
        }

        pageModel.setCurrPage(currPage);
        pageModel.setData(data);
        pageModel.setPageSize(pageSize);
        pageModel.setTotalCount(totalCount);
        pageModel.setTotalPage(totalPage);
        pageModel.setBegin(begin);
        pageModel.setEnd(end);

        return pageModel;
    }

    public static PageEntity<Map<String,Object>> createFocusPage
            (Integer currPage,List<Map<String,Object>> data,Integer totalCount){

        PageEntity<Map<String, Object>> pageModel = new PageEntity<>();
        pageModel.setCurrPage(currPage);

        // 页大小
        Integer pageSize = pageModel.getPageSize();

        //页数
        Integer totalPage = totalCount%pageSize == 0 ? totalCount/pageSize : (totalCount/pageSize) + 1;

        Integer size = pageModel.getSize();

        //起始页
        Integer begin = currPage%size == 0 ? currPage - size + 1  : currPage - (currPage%size) + 1;

        //末页
        Integer end = begin + size - 1;
        if(end > totalPage){
            end = totalPage;
        }
        pageModel.setCurrPage(currPage);
        pageModel.setData(data);
        pageModel.setPageSize(pageSize);
        pageModel.setTotalCount(totalCount);
        pageModel.setTotalPage(totalPage);
        pageModel.setBegin(begin);
        pageModel.setEnd(end);

        return pageModel;
    }

    public static PageEntity<Article> createArtPage(Integer currPage, List<Article> data, Integer totalCount){
        PageEntity<Article> pageModel = new PageEntity<>();
        pageModel.setCurrPage(currPage);

        // 页大小
        Integer pageSize = pageModel.getPageSize();

        //页数
        Integer totalPage = totalCount%pageSize == 0 ? totalCount/pageSize : (totalCount/pageSize) + 1;

        Integer size = pageModel.getSize();

        //起始页
        Integer begin = currPage%size == 0 ? currPage - size + 1  : currPage - (currPage%size) + 1;

        //末页
        Integer end = begin + size - 1;
        if(end > totalPage){
            end = totalPage;
        }
        pageModel.setCurrPage(currPage);
        pageModel.setData(data);
        pageModel.setPageSize(pageSize);
        pageModel.setTotalCount(totalCount);
        pageModel.setTotalPage(totalPage);
        pageModel.setBegin(begin);
        pageModel.setEnd(end);

        return pageModel;
    }

    public static PageEntity<Meeting> createMeetPage(Integer currPage, List<Meeting> data, Integer totalCount){
        PageEntity<Meeting> pageModel = new PageEntity<>();
        pageModel.setCurrPage(currPage);

        // 页大小
        Integer pageSize = pageModel.getPageSize();

        //页数
        Integer totalPage = totalCount%pageSize == 0 ? totalCount/pageSize : (totalCount/pageSize) + 1;

        Integer size = pageModel.getSize();

        //起始页
        Integer begin = currPage%size == 0 ? currPage - size + 1  : currPage - (currPage%size) + 1;

        //末页
        Integer end = begin + size - 1;
        if(end > totalPage){
            end = totalPage;
        }
        pageModel.setCurrPage(currPage);
        pageModel.setData(data);
        pageModel.setPageSize(pageSize);
        pageModel.setTotalCount(totalCount);
        pageModel.setTotalPage(totalPage);
        pageModel.setBegin(begin);
        pageModel.setEnd(end);

        return pageModel;
    }
}
